const app = getApp();
var utilMd5 = require('../util/md5.js');


Page({
	data: {
    bannerList: [{}],
    tempBannerList:[{}],
    motto: 'Hello World',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    navData: [],
    pddshopList:[],
    shopList:[],
    currentTab: 0,
    navScrollLeft: 0,
    page:1,
    //准备数据 
    menu: {
    },
    goodsList:[],
    index:'',
  },
	onLoad: function(options) {
    var that = this;
    that.setData({
      index:options.index
    })
    if(options.index==1){
      wx.setNavigationBarTitle({
        title: '今日爆款',
      })
      that.getGoodsList(that.data.index);
    } else if (options.index == 0) {
      wx.setNavigationBarTitle({
        title: '1.9包邮',
      })
      that.getGoodsList(that.data.index);
    } else if (options.index == 2) {
      wx.setNavigationBarTitle({
        title: '拼多多',
      })
      that.getGoodsList(that.data.index);
    } else if (options.index == 5) {
      wx.setNavigationBarTitle({
        title: '精选商城',
      })
      that.getPddList();
    } else if (options.index == 6) {
      wx.setNavigationBarTitle({
        title: '活动列表',
      })
      // that.getGoodsRecommend();
    }
    
    wx.getStorage({
      key: 'token',
      success: function (res) {
        if(res.data!=''){
            app.globalData.token=res.data
        }
      },
    })

    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse) {
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }
    wx.getSystemInfo({
      success: (res) => {
        this.setData({
          pixelRatio: res.pixelRatio,
          windowHeight: res.windowHeight,
          windowWidth: res.windowWidth
        })
      },
    })       
  },

  //获取商品列表
  getPddList() {
    wx.showLoading({
      title: '加载中',
      mask: false
    })
    var that = this;
    wx.request({
      url: app.globalData.api + '/app.php?c=Pdd&a=getGoodsList',
      data:
      {
        token: app.globalData.token,
        // keyword: '母婴',
        opt_id: 0,
        page: that.data.page,
        page_size: 30
      },
      headers: {
        'platform': '' + app.globalData.platform,
        'version': '' + app.globalData.version,
      },
      method: 'POST',
      dataType: 'json',
      success: function (res) {
        console.log(res);
        wx.hideLoading();
        if (res.data.code == 0) {
          that.setData({
            shopList: that.data.shopList.concat(res.data.data.list)
          })
        } else {
          wx.showToast({
            title: '' + res.data.msg,
            icon: 'none'
          })
        }
      }, fail: function () {
        wx.hideLoading();
      }
    });
  },

  //运营频道商品查询-推荐商品
  getGoodsRecommend() {
    wx.showLoading({
      title: '加载中',
      mask: false
    })
    var that = this;
    wx.request({
      url: app.globalData.api + '/app.php?c=Haodanku&a=supersearch',
      data:
      {
        token: app.globalData.token,
        keyword: '食品',
        back: 10,
        is_coupon: 1
      },
      header: {
        "Content-Type": "application/x-www-form-urlencoded",
        'platform': '' + app.globalData.platform,
        'version': '' + app.globalData.version,
      },
      method: 'POST',
      dataType: 'json',
      success: function (res) {
        console.log(res);
        wx.hideLoading();
        if (res.data.code == 0) {
          that.setData({
            shopList: that.data.shopList.concat(res.data.data.list)
          })
        } else {
          wx.showToast({
            title: '' + res.data.msg,
            icon: 'none'
          })
        }
      }, fail: function () {
        wx.hideLoading();
      }
    });
  },
  //获取商品列表
  getGoodsList(index) {
    wx.showLoading({
      title: '加载中',
      mask: false
    })
    var that = this;
       //获取当前时间戳  
    var timestamp = Date.parse(new Date());  
    timestamp = timestamp / 1000;  
    console.log(index)
    var offsets=that.data.page;
    console.log(offsets)
    var code = app.globalData.pdd_client_secret + 'channel_type' + index + 'client_id' + app.globalData.pdd_client_id + 'data_typeJSONlimit40offset' + offsets+'timestamp' + timestamp + 'typepdd.ddk.goods.recommend.get' + app.globalData.pdd_client_secret;
    console.log(code)
    var MD5code = utilMd5.md5(code);   //code需要加密的数据
    wx.request({
      url: app.globalData.apiPdd + '',
      data: {
        offset: offsets,
        limit:40,
        data_type: 'JSON',
        client_id: '' + app.globalData.pdd_client_id,
        timestamp: '' + timestamp,
        channel_type:''+index,
        type:'pdd.ddk.goods.recommend.get',
        sign: '' + MD5code.toUpperCase()
      },
      header: {
        'Content-type':'application/x-www-form-urlencoded',
        'platform': '' + app.globalData.platform,
        'version': '' + app.globalData.version,
      },
      method: 'POST',
      dataType: 'json',
      success: function (res) {
        console.log(res);
        wx.hideLoading();
          that.setData({
            shopList: that.data.shopList.concat(res.data.goods_basic_detail_response.list)
          })
      },
      fail: function () {
        wx.hideLoading();
      }
    });
  },

  toTbkdetail:function(e){
    if (app.globalData.token != '') {
      wx.navigateTo({
        url: '/pages/shop/pdd-item?id=' + e.currentTarget.dataset.id,
      })
    } else {
      wx.navigateTo({
        url: '/pages/mine/login',
      })
    }
    
  },
  toDetail:function(e){
    console.log(e)
  },
onShow(){

},
  /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
  onPullDownRefresh: function () {
    var that=this;
    wx.showNavigationBarLoading(); //在标题栏中显示加载图标
    setTimeout(function () {
      wx.stopPullDownRefresh(); //停止加载
      wx.hideNavigationBarLoading(); //隐藏加载icon
    }, 2000)
    this.setData({
      page: 1,
      shopList: []
    })
    that.getGoodsList(that.data.index);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    // this.getCartList()
    // var that = this;
    // that.setData({
    //   page: that.data.page + 1
    // })
    // that.getGoodsList(that.data.index);
  },

});
